package com.tbit.uqbike.webmanager.controller;

import com.tbit.uqbike.object.pojo.dto.BatteryMessageDownloadDTO;
import com.tbit.uqbike.object.pojo.dto.BatteryMsgDTO;
import com.tbit.uqbike.object.pojo.vo.BatchOperationMsgVO;
import com.tbit.uqbike.object.pojo.vo.BatteryMsgVO;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.common.entity.view.PageResult;
import com.tbit.common.entity.view.Result;
import com.tbit.uqbike.service.business.BatteryMsgService;
import com.tbit.uqbike.webmanager.util.Assert;
import com.tbit.uqbike.webmanager.util.csv.annotations.AsyncDownload;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.*;

/**
 * 电池厂商信息
 *
 * @Author:BUGTian
 * @DATE: 2022/5/13
 */
@RestController
@RequestMapping("/batteryMsg")
@Validated
public class BatteryMsgController {

    @Autowired
    private BatteryMsgService batteryMsgService;

    /**
     * 分页查询电池厂商信息
     *
     * @param accountId      区域id
     * @param page           当前页码
     * @param size           每页显示的行
     * @param batteryNO      电池编号
     * @param batteryModelId 电池型号id
     * @param startTime      开始时间
     * @param endTime        结束时间
     * @param state          电池状态
     * @return
     */
    @GetMapping("/queryPage")
    public Result<PageResult<BatteryMsgVO>> queryPage(@NotNull Integer accountId, @NotNull Integer page, @NotNull Integer size, String batteryNO, Integer batteryModelId, Date startTime, Date endTime, Integer state, String userCode) {
        if (page == null || size == null) {
            return Result.error("分页参数有误");
        }
        PageResult<BatteryMsgVO> pageResult = batteryMsgService.queryPage(accountId, page, size, batteryNO, batteryModelId, startTime, endTime, state, userCode);
        return Result.success(pageResult);
    }

    @GetMapping("/export")
    @AsyncDownload(defaultFileName = "电池厂商信息")
    public Result<String> batteryMessageDownloadCsv(BatteryMessageDownloadDTO downloadDTO) {
        String url = batteryMsgService.batteryMessageDownloadCsv(downloadDTO);
        return Result.success(url);
    }

    /**
     * 电池厂商信息导出excel
     * @param downloadDTO 入参
     * @return excel
     */
    @GetMapping("/exportExcel")
    public Result<Object> batteryMessageDownload(BatteryMessageDownloadDTO downloadDTO) {
        return batteryMsgService.batteryMessageDownload(downloadDTO);
    }

    /**
     * 批量新增电池
     *
     * @param batteryMsgDTO
     * @return
     */
    @PostMapping("/batchAdd")
    public Result<BatchOperationMsgVO<String>> batchAdd(@RequestBody BatteryMsgDTO batteryMsgDTO, HttpServletRequest request) {
        LoginInfo login = BaseController.getLogin(request);
        return batteryMsgService.batchAdd(batteryMsgDTO, login);
    }


    /**
     * 删除电池
     *
     * @param batteryIds 电池id，多个用,隔开
     * @return
     */
    @DeleteMapping("/delete")
    public Result delete(HttpServletRequest request, Integer... batteryIds) {
        LoginInfo login = BaseController.getLogin(request);
        return batteryMsgService.delete(login, batteryIds);
    }

    /**
     * 删除电池
     *
     * @param batteryNos 电池id，多个用,隔开
     * @return .
     */
    @PostMapping("/deleteByNo")
    public Result<BatchOperationMsgVO<String>> deleteByBatteryNo(HttpServletRequest request, String batteryNos,Integer accountId) {
        LoginInfo login = BaseController.getLogin(request);
        if (StringUtils.isBlank(batteryNos)) {
            return Result.error("请填写电池编号");
        }
        if (accountId == null) {
            return Result.error("请选择运营区域");
        }
        return batteryMsgService.deleteByBatteryNo(login, batteryNos,accountId);
    }

    /**
     * 批量操作电池状态
     * @param request request
     * @param type 1:标记丢失 2:删除丢失标记
     * @param accountId 区域id
     * @param batteryNo 电池编号
     * @return result
     */
    @PostMapping("/lose")
    public Result<BatchOperationMsgVO<String>> lose(HttpServletRequest request, Integer type, Integer accountId,
                                                    String batteryNo, @Size(max = 480, message = "备注长度不能超过480") String remark) {
        LoginInfo login = BaseController.getLogin(request);
        Assert.notNulls("参数有误", type, accountId, accountId, batteryNo, remark);
        return batteryMsgService.lose(login, accountId, type, batteryNo, remark);
    }

}
